

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Linkedlist:
    def __init__(self):
        self.head = Node(0)

    #头插法
    def head_insert(self,data):
        new_node=Node(data)
        new_node.next=self.head.next
        self.head.next=new_node
        self.head.data+=1

    #尾插法
    def tail_insert(self,data):
        new_node=Node(data)
        node=self.head.next
        while node.next:
            node=node.next
        node.next=new_node
        new_node.next=None
        self.head.data-=1

    #删除
    def delete_list(self,data):
        pre=self.head
        current=self.head.next
        while current:
            if current.data==data:
                pre.next=current.next
                del current
                self.head.data-=1
                break
            else :
                pre=current
                current=current.next


    #遍历
    def printlist(self):
        node=self.head.next
        while node:
            print(f"{node.data}")#print(f"{node.data}")自带换行，如果加上end=“”就不会换行了
            node=node.next






if __name__=="__main__":
    mylist=Linkedlist()
    mylist.head_insert(1)
    mylist.head_insert(2)
    mylist.tail_insert(3)
    mylist.delete_list(2)
    mylist.printlist()



